
import 'package:flutter/material.dart';
import 'package:jd_shopping/services/ScreenAdaper.dart';
import 'package:jd_shopping/pages/services/SearchServices.dart';

class Search extends StatefulWidget {
  @override
  _SearchState createState() => _SearchState();
}

class _SearchState extends State<Search> {

  List <String>_hotList = [
    '女装',
    '笔记本电脑',
    '飞机',
    '马自达',
    '卡帝乐',
    '男装',
    '笔记本电脑',
    '飞机',
    '马自达',
  ];
  var _keywords;
  List _historyListData = [];
  TextEditingController controller = TextEditingController();

  _getHistoryData() async {
    var _historyListData = await SearchServices.getHistoryList();
    setState(() {
      this._historyListData=_historyListData;
    });
  }

  _showAlertDialog(keywords) async{
     await showDialog(
        barrierDismissible:false,   //表示点击灰色背景的时候是否消失弹出框
        context:context,
        builder: (context){
          return AlertDialog(
            title: Text("提示信息!"),
            content:Text("您确定要删除吗?") ,
            actions: <Widget>[
              FlatButton(
                child: Text("取消"),
                onPressed: (){
                  print("取消");
                  Navigator.pop(context,'Cancle');
                },
              ),
              FlatButton(
                child: Text("确定"),
                onPressed: () async{            
                  //注意异步      
                  await SearchServices.clearHistoryList();
                  this._getHistoryData();
                  Navigator.pop(context,"Ok");
                },
              )
            ],

          );
        }
     );

    //  print(result);

  }

  @override
  void initState() {
    super.initState();
    this._getHistoryData();
  }

  Widget _searchHotlist(){
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Container(
          alignment: Alignment.centerLeft,
          padding: EdgeInsets.only(left:ScreenAdaper.height(10)),
          height: ScreenAdaper.height(60),
          child: Text('热门搜索'),
        ),
        Divider(height:1),
         Wrap(
                children:_hotList.map((value){
                  return InkWell(
                    child:Container(
                    padding: EdgeInsets.all(10),
                    margin: EdgeInsets.all(10),
                    decoration: BoxDecoration(
                        color: Color.fromRGBO(233, 233, 233, 0.9),
                        borderRadius: BorderRadius.circular(10)),
                       child: Text("$value"),
                     ),
                     onTap: (){
                       setState(() {
                        this._keywords =  value;
                        controller.text = value;
                        pragma(value);
                       });
                     },
                  );
              }).toList()
            ),
           Divider(height:1),
          ],
    );
  }


  Widget _searchList(){
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Container(
          alignment: Alignment.centerLeft,
          padding: EdgeInsets.only(left:ScreenAdaper.height(10)),
          height: ScreenAdaper.height(60),
          child: Text('历史记录'),
        ),
        Divider(height:1),
        Column(
              children: this._historyListData.map((value){
                return  Column(
                  children: <Widget>[
                    Container(
                      child:Column(
                          children: [
                            ListTile(
                            title: Text('$value',style:TextStyle(color: Colors.black54,fontSize:ScreenAdaper.size(30))),
                            onTap:(){
                              setState(() {
                                 this._keywords =  value;
                                 controller.text = value;
                                 pragma(value);
                               });
                            },
                      ),
                      Divider()
                      ],
                      ),
                    )
                  ],
                );
              }).toList()
          ),
          SizedBox(height: 20),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              InkWell(
                onTap: () {
                  _showAlertDialog( this._keywords);
                },
                child: Container(
                  width: ScreenAdaper.getScreenWidth()-40,
                  height: ScreenAdaper.height(64),
                  decoration: BoxDecoration(
                      border: Border.all(color: Colors.black45, width: 1)),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[Icon(Icons.delete), Text("清空历史记录")],
                  ),
                ),
              )
            ],
          )
        ],
       );
  }

  Widget _appBar(){
   return  AppBar(
          title: Container(
            child: TextField(
              controller: controller,
              autofocus: true,
              decoration: InputDecoration(
                  border: OutlineInputBorder(
                      borderRadius: BorderRadius.circular(30),
                      borderSide: BorderSide.none
                      )
                  ),
              onChanged: (value) {
                this._keywords = value;
              },
              onSubmitted:(value){
                 SearchServices.setHistoryData(this._keywords);
                 Navigator.pushReplacementNamed(context, '/searchproductList',arguments: {
                      "cid":'',
                      "title":'商品列表',
                      'keywords':this._keywords
                    });
              },
            ),
            height: ScreenAdaper.height(68),
            decoration: BoxDecoration(
                color: Color.fromRGBO(233, 233, 233, 0.8),
                borderRadius: BorderRadius.circular(30)),
          ),
          actions: <Widget>[
            InkWell(
              child: Container(
                height: ScreenAdaper.height(68),
                width: ScreenAdaper.width(80),
                child: Row(
                  children: <Widget>[Text("搜索")],
                ),
              ),
              onTap: () {
               SearchServices.setHistoryData(this._keywords);
               Navigator.pushReplacementNamed(context, '/searchproductList',arguments: {
                      "cid":'',
                      "title":'商品列表',
                      'keywords':this._keywords
                    });
              },
            )
          ],
        );
  }

  @override
  Widget build(BuildContext context) {
     ScreenAdaper.init(context);
       return Scaffold(
         appBar:_appBar(),
         body: Container(
            child: ListView(
              children: <Widget>[
                _searchHotlist(),
                _searchList()
              ],
            ),
         )
       );
     }
  }